



package com.example.xunua_zhinengjiaotong;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_zhinengjiaotong.adapter.TrolleyMessageAdapter;
import com.example.xunua_zhinengjiaotong.utils.CarLogBean;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.OkHttpClient;

public class Trolley_RecordActivity extends AppCompatActivity {
    private ImageView img_back;
    private Spinner spinner;
    private Button btn_chaxun;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();
    private ArrayList<CarLogBean> loglist;
    private ListView lv;
    private TextView tv_none;
    private TrolleyMessageAdapter myAdapter;


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == -1) {
                tv_none.setVisibility(View.VISIBLE);
            }
            if (msg.what == 0) {
                if (loglist.size()<=0){
                    tv_none.setVisibility(View.VISIBLE);
                }else {
                    tv_none.setVisibility(View.GONE);
                    lv.setVisibility(View.GONE);
                }
                myAdapter=new TrolleyMessageAdapter(loglist,Trolley_RecordActivity.this);
                myAdapter.notifyDataSetChanged();
                lv.setAdapter(myAdapter);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trolley__record);
        initView();
    }

    private void initView() {
        loglist=new ArrayList<>();
        myApp= (MyApp) getApplication();
        tv_none=findViewById(R.id.trolley_tvlog_none);
        lv=findViewById(R.id.trolley_log_lv);
        img_back=findViewById(R.id.trolley_record_img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        spinner=findViewById(R.id.trolley_record_spinner);
        spinner.setAdapter(new ArrayAdapter<>(Trolley_RecordActivity.this,R.layout.support_simple_spinner_dropdown_item,new String[]{"一号小车","二号小车","三号小车","四号小车"}));
        btn_chaxun=findViewById(R.id.trolley_record_btn_chaxun);
        btn_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id= (int) (spinner.getSelectedItemId()+1);
                getCarRecord(id+"");
            }
        });
    }
    private void getCarRecord(final String id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray getCarAccountRecord = util.connectServer_JsonArray("{\"CarId\":" + id + ",\"CostType\":\"All\"}", myApp, "GetCarAccountRecord");
                    loglist.clear();
                    for (int i = 0; i < getCarAccountRecord.length(); i++) {
                        String s = getCarAccountRecord.getString(i);
                        JSONObject jsonObject = new JSONObject(s);
                        String addr = jsonObject.getString("Addr");//地址
                        String cost = jsonObject.getString("Cost");//花费
                        String carId = jsonObject.getString("CarId");//编号
                        String time = jsonObject.getString("Time");//时间
                        CarLogBean bean=new CarLogBean(addr,cost,carId,time);
                        loglist.add(bean);
                    }
                    handler.sendEmptyMessage(0);
                }catch (Exception e){
                    handler.sendEmptyMessage(-1);
                    e.printStackTrace();
                }
            }
        }).start();
    }
    @Override
    public void onBackPressed() {
        // super.onBackPressed();
    }
}
